/**
* @depends jquery.ui.js
*/

(function($) {

	$.widget( 'ui.widget_flow_genericflow_writer', {
		
		_init: function() {

			var $this = this;
			var flow_id = this.element.attr( 'flow_id' );
			var poster = this.element;
			poster.addClass( 'ui-default' );
			var textarea = this._getTextarea()
				.blur( function( e ) {
					if( textarea.hasClass( 'Down' ) || $('.Join', this.element).length ) {
						textarea.removeClass( 'Down' );
						e.stopImmediatePropagation();
						e.preventDefault();
						textarea.focus();
					}
					else {
						poster.addClass( 'ui-default' );
					}
				} )
				.focus( function( e ) {
					poster.removeClass( 'ui-default' );
				} )
				.textarea( {
					defaultText : 'Anything to share ?'
				} )
				.keyup( function( e ) {
					if( e.keyCode == $.ui.keyCode.ENTER 
						|| ((e.keyCode == 'V'.charCodeAt(0)) && (e.ctrlKey || e.metaKey)) ) {
						var content = $.trim( textarea.attr( 'value' ) );
						var regex = /((?:(?:http|ftp):\/\/|www\.)\S+)$/gi;
						var results = content.match( regex );
						if( results && results.length ) {
							if( !$('.Join.Link[url=' + encodeURI(results[0])  + ']').length ) {
								$this._openLink( results[0] );
							}
						}
					}
				} );

			var button = $('a[action=share]', this.element)
				.mousedown( function() {
					textarea.addClass( 'Down' );
				} )
				.click( function() {
					textarea.blur();
					$.wajax( 'Flow_GenericFlow_Item_Add', $this._getData(), function( json ) {
							if( json.status != 'success' ) {
								return false;
							}
							$('.Join', $this.element).remove();
							textarea.attr( 'value', '' );
							textarea.blur();
							var post = $(json.content);
							post.hide();
							$this.element.closest( '.Widget_Flow' ).find( '> ul').prepend( post );
							post.slideDown( 'fast' );
							post.find( '.Widget_Flow_GenericFlow_Item' ).widget_flow_genericflow_item();
							MathJax.Hub.Queue( [ 'Typeset', MathJax.Hub ] );
						} );
				} );

			poster.find( 'img[action=link]')
				.mousedown( function() {
					textarea.addClass( 'Down' );
				} )
				.click( function() {
					$this._openLink();
				} );
		},
		
		_getTextarea: function() {
			return $('textarea:not(.ui-textarea-sizer)', this.element);
		},
		
		_getData: function() {
			var textarea = this._getTextarea();
			var data = {
				id: this.element.attr( 'flow_id' ),
				message: textarea.hasClass( 'ui-default' ) ? undefined : textarea.attr( 'value' ),
			};
			var i = 0;
			$('.Join', this.element).each( function() {
				var d = $(this).data( 'form' );
				for( var j in d ) {
					data['joins[' + i + '][' + j + ']'] = d[j];
				}
				i++;
			} );
			return data;
		},
		
		_openLink: function( url ) {

			var attach = $('.Attach', this.element );
			$.wajax( 'Flow_GenericFlow_Item_Join_LinkBox', function( json ) {
				if( json.status != 'success' ) {
					return false;
				}
				var box = $(json.content);
				box.insertBefore( attach );
				box.widget_flow_genericflow_linker( {
					url: url
				} );
			}, true );
		}

	} );
	
	$.widget( 'ui.widget_flow_genericflow_linker', {
	
		_init: function() {
			
			var $this = this;
			var input = $('input', this.element);
			input.input( {
				defaultText: 'http://'
			} );
			
			$('.Delete.Icon', this.element).click( function() {
				var poster = $this.element.closest( '.Poster' );
				$this.element.remove();
				poster.find( 'textarea.ui-textarea:not(.ui-default)' ).focus();
			} );
			
			$('a.Button', this.element).click( function() {
				$this.attach();
			} );

			$('form', this.element).submit( function( e ) {
				e.preventDefault();
				$this.attach();
				return false;
			} );
			
			if( this.options.url ) {
				input.attr( 'value', this.options.url );
				this.attach();
			}
		},
		
		attach: function() {
		
			var $this = this;
			var input = $('input', this.element);
			var value = input.attr( 'value' );
			var content = $('.Content', this.element);
			content.addClass( 'Loading' );
			content.html( '<img src="images/loading.gif" alt="Loading" />' );

			this.element.attr( 'url', encodeURI(value) );
			this.element.removeClass( 'Visible' );
			$.wajax( 'Flow_GenericFlow_Item_Join_Link', {
				link: value
			}, function( json ) {
				if( json.status != 'success' ) {
					return;
				}
				content.html( json.content.content );
				var form = json.content.data;
				form.join = 'link';
				$this.element.data( 'form', form );
				content.removeClass( 'Loading' );
			} );
		},
		
		options: {
			url: ''
		}
	} );

})(jQuery);
